//
//  pictViewController.swift
//  Status
//
//  Created by 花轮 on 15/5/22.
//  Copyright (c) 2015年 花轮. All rights reserved.
//

import UIKit

class PictViewController: UICollectionViewController {

    var imageList = [UIImage]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "addImage:", name: LKAddImageNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "deleImage:", name: LKDeleImageNotification, object: nil)
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    func addImage(n: NSNotification) {
        
        let cell = n.object as! SeleImgCell
        
        if cell.image != nil{
            return
        }
        
        if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
            println("不支持访问相册")
            return
        }
        
        // 选择照片
        let picker = UIImagePickerController()
        
        picker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
        picker.delegate = self
        presentViewController(picker, animated: true, completion: nil)
    }
    func deleImage(n: NSNotification) {
//        println(n)
        
        let cell = n.object as! SeleImgCell
        
        let indexPath = collectionView?.indexPathForCell(cell)
        
        imageList.removeAtIndex(indexPath!.item)
        
        collectionView?.reloadData()
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return imageList.count + 1
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("seleImgCell", forIndexPath: indexPath) as! SeleImgCell
    
        if indexPath.item < imageList.count
        {
            cell.image = imageList[indexPath.item]
        }
        else {
            cell.image = nil
        }
    
        return cell
    }
}
extension PictViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
//        println(image)
        imageList.append(image.scaleImg(width: 320))
        collectionView?.reloadData()
        dismissViewControllerAnimated(true, completion: nil)
    }
}





// MARK: - cell
let LKAddImageNotification = "LKAddImageNotification"
let LKDeleImageNotification = "LKDeleImageNotification"
class SeleImgCell: UICollectionViewCell{
    @IBOutlet weak var deleBtn: UIButton!
    @IBOutlet weak var imageBtn: UIButton!
    @IBAction func addBtn() {
        NSNotificationCenter.defaultCenter().postNotificationName(LKAddImageNotification, object: self)
    }
    @IBAction func deleClick(){
        NSNotificationCenter.defaultCenter().postNotificationName(LKDeleImageNotification, object: self)
    }
    
    var image: UIImage? {
        didSet {
            
            deleBtn.hidden = (image == nil)
            if image != nil
            {
                imageBtn.setImage(image, forState: UIControlState.Normal)
                imageBtn.setBackgroundImage(nil, forState: UIControlState.Normal)
            }
            else{
                imageBtn.setBackgroundImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
                imageBtn.setBackgroundImage(UIImage(named: "compose_pic_add_highlighted"), forState: UIControlState.Highlighted)
                imageBtn.setImage(nil, forState: UIControlState.Normal)
            }
            
        }
    }
    
    override func awakeFromNib() {
        imageBtn.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
    }
}